package org.samples.support;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelDemo {

	public static void main(String[] args){
		String path = "D://download//test.xlsx";
		try {
			InputStream is = new FileInputStream(path);
			XSSFWorkbook workbook =new XSSFWorkbook(is);
			for(int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++){
				XSSFSheet sheet = workbook.getSheetAt(numSheet);
				if(sheet == null) continue;
				for(int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++){
					XSSFRow row = sheet.getRow(rowNum);
					if(row != null){
						XSSFCell userName = row.getCell(0);
						XSSFCell password = row.getCell(1);
						XSSFCell age = row.getCell(2);
						XSSFCell sex = row.getCell(3);
						XSSFCell email = row.getCell(4);
						XSSFCell remark = row.getCell(5);
						System.out.println(getValue(userName));
						System.out.println(getValue(password));
						System.out.println(getValue(age));
						System.out.println(getValue(sex));
						System.out.println(getValue(email));
						System.out.println(getValue(remark));
					}
				}
			}
			workbook.close();
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("static-access")
	private static String getValue(XSSFCell cell){
		if(cell == null) return "";
		if(cell.getCellType() == cell.CELL_TYPE_BOOLEAN){
			return String.valueOf(cell.getBooleanCellValue());
		}else if(cell.getCellType() == cell.CELL_TYPE_NUMERIC){
			DecimalFormat df = new DecimalFormat("0");
			return df.format(cell.getNumericCellValue());
		}else{
			return String.valueOf(cell.getStringCellValue());
		}
	}
	
}
